package com.bible.test.pad.activity;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Toast;

import com.bible.fire.R;
import com.bible.fire.bean.common.UploadFile;
import com.bible.fire.local.AppContext;
import com.bible.fire.pad.activity.BaseActivity;
import com.bible.fire.util.SystemConstant;
import com.bible.fire.util.TimeUtil;
import com.bible.fire.util.http.HttpClientHelper;

public class HttpSpeedTestActivity extends BaseActivity {
	
	//private static String requestUrl = "http://www.vtobank.com/sunflowbible/upload";
	private static String uploadUrl = "http://www.vtobank.com/sunflowerbible/upload.do";
	private static String downloadUrl = "http://www.vtobank.com/sunflowerbible/download/";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_test);
		mListView = (ListView)findViewById(R.id.listView);
		// 添加ListItem，设置事件响应
				List<String> data = new ArrayList<String>();
				for (int i = 0; i < mStrDemos.length; i++) {
					data.add(mStrDemos[i]);
				}
		        mListView.setAdapter((ListAdapter) new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,data));
		        mListView.setOnItemClickListener(new OnItemClickListener() {  
		            public void onItemClick(AdapterView<?> arg0, View v, int index, long arg3) {  
		            	onListItemClick(index);
		            }  
		        });
	}
	
	private Handler handler = new Handler(){
		@Override
		 public void handleMessage(Message m){
			dismissProgressDialog();
			try {
				if( m.what == 1 ){
    				String startTime = m.getData().getString("startTime");
    				String returnvalue = m.getData().getString("result");
    				if("OK".equals( returnvalue ) ){
    					if( startTime != null && startTime.length() > 0 ){
        					SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");
        					Map map = TimeUtil.getDuration(sdf.parse(startTime), new Date());
        					String duration = map.get("d") + "天" + map.get("h") + "小时" + map.get("m") + "分钟" + map.get("s") + "秒";
        					showAlertDialog( "操作成功,耗时" + duration );
            				//Toast.makeText(HttpSpeedTestActivity.this, "操作成功,耗时" + duration, Toast.LENGTH_LONG).show();
        				}else{
        					Toast.makeText(HttpSpeedTestActivity.this, "操作成功", Toast.LENGTH_LONG).show();
        				}   
    				}else{
    					Toast.makeText(HttpSpeedTestActivity.this, "操作失败", Toast.LENGTH_LONG).show();
    				}        				     				
    			}else if( m.what == 0 ){
    				Toast.makeText(HttpSpeedTestActivity.this, "操作失败", Toast.LENGTH_LONG).show();
    			}
			} catch (Exception e) {
				e.printStackTrace();
			}   			
		}
	};
	
	void onListItemClick(int index) {
    	this.showProgressDialog();
    	if( index < 6 ){//上传
    		Map<String, Object> paramMap = new HashMap<String, Object>();
        	List<UploadFile> fileList = new ArrayList<UploadFile>();
        	String[] subfiles = files[index].split(",");
        	for( String subfile : subfiles ){
        		UploadFile ufile = new UploadFile();
            	ufile.setParamName("uploadFile");
            	ufile.setFilepath( SystemConstant.FILE_UPLOAD_PATH + subfile);
            	fileList.add( ufile );
        	}    	
        	boolean result = HttpClientHelper.uploadFile(uploadUrl, paramMap, fileList, handler);
    	}else{
    		Map<String,Object> paramMap = new HashMap<String,Object>();
    		String fileformat = files[index].substring(files[index].indexOf(".")+1, files[index].length());
    		boolean result = HttpClientHelper.downloadFile( new String[]{downloadUrl + files[index]}, new String[]{fileformat}, paramMap, handler);
    	}
    	
    }

	
	@Override
	// 建议在APP整体退出之前调用Api的destroy()函数，不要在每个activity的OnDestroy中调用，
    // 避免Api重复创建初始化，提高效率
	protected void onDestroy() {
		AppContext app = (AppContext)this.getApplication();
		if (app.mBMapManager != null) {
			app.mBMapManager.destroy();
			app.mBMapManager = null;
		}
		super.onDestroy();
	}
	
	ListView mListView = null;
    String mStrDemos[] = {
    		"上传单个约1M文件",
            "上传单个约5M文件",
            "上传单个约10M文件",
            "上传单个约30M文件",
            "上传单个约50M文件",
            "上传多个文件(合计100M)",
            "下载单个约1M文件",
            "下载单个约5M文件",
            "下载单个约10M文件",
            "下载单个约30M文件",
            "下载单个约50M文件",
    };
    
    String files[] = {
    		"1M.jar",
    		"5M.zip",
    		"10M.zip",
    		"30M.rar",
    		"50M.rar",
    		"10M.zip,30M.rar,50M.rar",
    		"1M.jar",
    		"5M.zip",
    		"10M.zip",
    		"30M.rar",
    		"50M.rar",
    };
    
}
